window.search = function(event) {
  var str = document.getElementById("lineedit").value;
  if (str == "")
    str = "免费";

  // 双击，带着hash找到url，播放
  window.handleHashResponse = function (data) {
    console.log("Song Info Response:", data);
    const music_url = data.url;
    const music_pic = data.album_img;
    const audio = document.getElementById("myAudio");
    document.getElementById("myAudio").src = music_url;
    document.getElementById("cur_song").innerHTML = data.fileName;
    audio.play();       //双击播放
    document.getElementById("shoubi").style.transform = "rotate(-15deg)";                    //手臂移动

    document.getElementById("play_buttom").style.backgroundImage = "url(/source/playing.png)";    //切换播放按钮状态
    true_pic_url = music_pic.split("/");

    //图片显示
    document.getElementById("img").src = 'https://imgessl.kugou.com/stdmusic/' + true_pic_url[5] + "/" + true_pic_url[6];
    document.getElementById("lyric_img").src = 'https://imgessl.kugou.com/stdmusic/' + true_pic_url[5] + "/" + true_pic_url[6]; document.getElementById("lyric_img").style.visibility = "";
    document.getElementById("bg").src = 'https://imgessl.kugou.com/stdmusic/' + true_pic_url[5] + "/" + true_pic_url[6]; document.getElementById("img").style.visibility = "";
    console.log('https://imgessl.kugou.com/stdmusic/' + true_pic_url[5] + "/" + true_pic_url[6]);
    console.log(music_pic);

    //歌词显示           这里采用的方法是用酷狗的hash json信息找网易云的歌词 艹
    //搜索方式，歌曲名,唯一性有问题
    if (data.fileName.length > 25) var search_key = data.songName;
    else var search_key = data.fileName;
    console.log(search_key);
    fetch('http://localhost:3000/api/netease_search?s=' + search_key)
      .then(res => res.json())
      .then(wyy_data => {
        console.log("网易云歌曲信息", wyy_data);
        var id = wyy_data.result.songs[0].id;
        console.log(id);
        fetch('http://localhost:3000/api/netease_search/lyric?id=' + id)
          .then(res => res.json())
          .then(lyric => {
            console.log("歌词", lyric.lyric);
            handle_lyric(lyric.lyric);
          });
      });
  };

  // 找hash
  window.handleKugouResponse = function (data) {
    console.log("JSONP Response:", data);

    music_hash = [];
    music_name = [];
    singer_name = [];
    album_name = [];
    for (let i = 0; i < data.data.info.length; i++) {
      music_name[i] = data.data.info[i].songname;
      singer_name[i] = data.data.info[i].singername;
      album_name[i] = data.data.info[i].album_name;
      music_hash[i] = data.data.info[i].hash;
    }

    const listElement = document.getElementById('music_list');
    if (!listElement) {
      console.error("Element with id 'music_list' not found.");
      return;
    }

    // 清空列表
    if (str != "") {
      const table = document.getElementById("music_list");
      while (table.rows.length > 1) {
        table.deleteRow(1);
      }
    }

    for (let i = 0; i < Math.min(16, music_name.length); i++) {
      var table = document.getElementById("music_list");
      var row = table.insertRow(i);
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      var cell3 = row.insertCell(2);
      var cell4 = row.insertCell(3);
      var cell5 = row.insertCell(4);
      cell1.innerHTML = i + 1;
      cell2.innerHTML = music_name[i];
      cell3.innerHTML = singer_name[i];
      cell4.innerHTML = album_name[i];

      //关于收藏音乐全局的按钮差分
      var favorites = getCookie("favorites") || "[]";
      if (favorites.search(music_hash[i]) != -1)
        cell5.innerHTML = `<button id="love_but${i}" style="background-color: transparent; cursor:pointer; border: none;" class="fa fa-heart" onclick="love_song(${i},1)"></button>`;
      else
        cell5.innerHTML = `<button id="love_but${i}" style="background-color: transparent; cursor:pointer; border: none;" class="fa fa-heart-o" onclick="love_song(${i},0)"></button>`;

      // 添加双击事件
      row.ondblclick = function () {
        console.log(`当前双击的音乐的 hash: ${music_hash[i]}`);
        // 动态加载歌曲信息
        const hash_script = document.createElement('script');
        currentIndex = i;
        hash_script.src = `http://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&format=jsonp&hash=${encodeURIComponent(music_hash[i])}&callback=handleHashResponse`;
        http://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&format=jsonp&hash=
        document.body.appendChild(hash_script);
        // 清理脚本
        hash_script.onload = () => document.body.removeChild(hash_script);
      };
      listElement.appendChild(row);
    }
  };
  // 动态加载搜索结果
  const script = document.createElement('script');
  script.src = `http://mobilecdn.kugou.com/api/v3/search/song?format=jsonp&keyword=${encodeURIComponent(str)}&callback=handleKugouResponse`;
  document.body.appendChild(script);

  // 清理脚本
  script.onload = () => document.body.removeChild(script);
}


console.log("屏幕高：" + screen.height);
console.log("屏幕宽：" + screen.width);

function phone_or_pc() {
  if (screen.height / screen.width >= 1) {
    window.location.href = "index_phone.html";
  }
}

window.onload = function () {
  phone_or_pc();
}

